﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace HYFloorPipeModule
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class ConnFloorPipes : HYNoTranExternalCommand
	{
		public override Result HYExecute()
		{
			string text = "连接地热盘管";
			Transaction transaction = new Transaction(base.Doc, text);
			SubTransaction subTransaction = null;
			try
			{
				FrmConnStyle frmConnStyle = new FrmConnStyle();
				if (frmConnStyle.ShowDialog() != DialogResult.OK)
				{
					return Autodesk.Revit.UI.Result.Cancelled;
				}
				transaction.Start();
				Reference reference = base.Sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new HYDiverstyFilter(), "请选择要连接的分集水器……");
				FamilyInstance elem = base.Doc.GetElement(reference) as FamilyInstance;
				this.AddEquipmentShareParam(base.UiApp);
				for (;;)
				{
					subTransaction = new SubTransaction(base.Doc);
					subTransaction.Start();
					reference = base.Sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new HYModelLineGroupFilter(), "请选择要连接的地热盘管模型组……");
					Group group = base.Doc.GetElement(reference) as Group;
					FloorPipeConnParam settingParam = frmConnStyle.GetSettingParam();
					ConnectFloorPipe connectFloorPipe = new ConnectFloorPipe(base.Doc, 20.0, elem, settingParam);
					IList<Connector> list = new List<Connector>();
					IList<Connector> list2 = new List<Connector>();
					connectFloorPipe.GetEquipmentConn(elem, ref list, ref list2);
					Reference trackRefer = null;
					if (list.Count <= 0 || list2.Count <= 0)
					{
						break;
					}
					if (frmConnStyle.GetConnStyle() == 1)
					{
						trackRefer = base.Sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, new YJKCurveLineFilter(), "请选择路由线路的起始段（距离地热盘管近的一端）……");
					}
					connectFloorPipe.DoConnect(group, trackRefer, list, list2);
					subTransaction.Commit();
				}
				YJKMessageBox.Information("未找到未连接的分集水器接口！");
				subTransaction.Commit();
				transaction.Commit();
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
                if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.Commit();
				}
                if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.Commit();
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Information(ex.Message);
                if (subTransaction != null && (int)subTransaction.GetStatus() != 3)
				{
					subTransaction.RollBack();
				}
                if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private string AddEquipmentShareParam(UIApplication uiApp)
		{
			string shareFilePath = Product.InstallLocation + "\\Data\\\\HYShared Parameters.txt";
			DefinitionFile definitionFile = YJKRevitTools.OpenShareParamFile(uiApp, shareFilePath);
			if (definitionFile == null)
			{
				return "共享参数文件不存在！";
			}
			CategorySet bdcategorySet = YJKRevitTools.GetBDCategorySet(uiApp, BuiltInCategory.OST_MechanicalEquipment);
			InstanceBinding elemBinding = YJKRevitTools.CreatElemBinding(uiApp, bdcategorySet, "instance") as InstanceBinding;
			return YJKRevitTools.SetShareParameter(uiApp, definitionFile, "机械设备", new List<string>
			{
				"已连接接口"
			}, elemBinding, (BuiltInParameterGroup)(-5000123)).ToString();
		}
	}
}
